// <copyright file="Program.cs" company="Sinclair Community College">
// Copyright (c) Sinclair Community College. All rights reserved.
// </copyright>

namespace SinclairCC.AppVSuite.DuplicateRootDetector
{
    using System.Windows.Forms;

    /// <summary>
    /// Provides the main entry point to the application.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [System.STAThread]
        public static void Main()
        {
            // A unique name for a mutex.
            string mutexName = "B1F49A46-794C-4c73-94CF-EE6F3584A924";
            
            // Whether the calling thread was granted initial ownership of the mutex.
            bool grantedOwnership = false;
            
            // Create a new Mutex object, requesting initial ownership.
            System.Threading.Mutex singleInstanceMutex = new System.Threading.Mutex(true, mutexName, out grantedOwnership);

            try
            {
                // If this thread was not granted ownership of the mutex, another instance of the application
                // must already have ownership. In this case, abandon this instance of the application. Otherwise,
                // proceed with starting the application.
                if (!grantedOwnership)
                {
                    System.Windows.Forms.MessageBox.Show("Another instance of this application is already running.", "Duplicate Root Detector", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                else
                {
                    // Enable visual styles for the application.
                    Application.EnableVisualStyles();

                    // Sets the application-wide default for the 
                    // System.Windows.Forms.ButtonBase.UseCompatibleTextRendering
                    // property defined on certain controls.
                    Application.SetCompatibleTextRenderingDefault(false);

                    // Runs a standard application message loop on the current thread,
                    // making the specified form visible.
                    Application.Run(new MainForm());
                }
            }
            finally
            {
                // Release all resources held by the mutex.
                singleInstanceMutex.Close();
            }
        }
    }
}